"""
@author: lyf
@file: 三创商城.py
@time: 2022/11/9 14:51
"""
print("欢迎来到三创商城")
num = input("请选择您的操作\n1.登录\n2.注册\n")
u = open('useradd','r+',encoding='utf-8')
p = open('passwd','r+',encoding='utf-8')
c = open('countyue','r+',encoding='utf-8')
s = open('shopping','w+',encoding='utf-8')
g = open('goods','r',encoding='utf-8')
useradd = u.read().split()
passwd = p.read().split()
countyue = c.read().split()
goods = g.read().splitlines()
shopping = s.read()
useradd_input = ""
passwd_input = ""
shooping_cart = {}
countyue = 500
countyue = int(countyue)
goods = [['coffee', 50],['tea', 150],['book', 50]]
cost=0
shooping_cart={}
if int(num) == 1:
    useradd_input = input("请输入用户名:\n")
    if useradd_input in useradd:
        passwd_input = input("请输入密码:\n")
    else:
        print("您输入的用户名不对，请注册")
    if passwd_input in passwd:
        print("查看商品列表\n",goods)
        while True:
            choice = int(input("请输入商品对应的编号，按9键结账：").strip())
            if choice in range(0, len(goods)):
                goods_name = goods[choice][0]
                goods_price = goods[choice][1]
                if int(countyue) > int(goods_price):
                    if goods_name in shooping_cart:
                        shooping_cart[goods_name]['count'] += 1
                        shooping_cart.append(shooping_cart)
                    else:
                        shooping_cart[goods_name] = {'price': goods_price, 'count': 1}
                    cost += goods_price
                    countyue -= goods_price
                    print(shooping_cart)
                else:
                    print("余额不足，请选择其他商品")
                    print("您现在账户余额还剩" + str(countyue) + "元")
            elif choice == 9:
                print(shooping_cart)
                print("花费" + str(cost) + "元")
                buy = input("是否结账(y/n):").strip()
                if buy == 'y':
                    shopping_cart = shooping_cart
                    print(shopping_cart)
                    print("账户余额还剩" + str(countyue) + "元")
                    c.write(str(countyue))
                    s.write(str(shooping_cart))
                    break
                elif buy == 'n':
                    print("您已取消订单，欢迎下次光临")
                    break
                else:
                    print("请正确输入")
                    continue
            else:
                print("请正确输入编号")
    else:
        print("您输入的密码不对:")
elif int(num) == 2:
    print("欢迎注册三创商城")
    useradd_save = input("请输入您的用户名:\n")
    u.write(useradd_save+'\n')
    passwd_save = input("请输入您的密码:\n")
    passwd_save2 = input("请再次输入您的密码:\n")
    if passwd_save == passwd_save2:
        p.write(passwd_save+'\n')
        print("注册成功")
    else:
        print("两次输入密码不一致")
u.close()
p.close()
c.close()
s.close()
g.close()